<?php

namespace addons\crontab\model;

use think\Model;
use think\Db;
use app\admin\model\inventory\Temporary;

//材料出库单 表头
class RdRecord11 extends Model
{
    protected $connection = "database.inc";
    protected $table = "RdRecord11";
    
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 定义时间戳字段名
    protected $createTime = 'dnmaketime';
    protected $updateTime = 'dnmodifytime';
    // 定义字段类型
    protected $type = [
    ];
    // 追加属性
    protected $append = [
    ];
    
    /*
     * 新增数据（表头）,返回表头对象,2拣货、11直接领料
     *   */
    public function insertMain($data,$pc_order_type = 2){
        if(!$data){
            exception('RdRecord11表头为空');
        }
        
        $model =  self::where('cCode',$data['code'])->find();
        if(!$model){
            $cBusType = '领料';
            $cSource = '库存';
            if($data['source_code']){
                $cSource = '领料申请单';
            }
            //表头
            $model = self::allowField(true)->create([
                'ID'=>Crontab::getAutoIDByTname('RdRecord11'),//收发记录主表标识   表头id 自增
                'bRdFlag'=> 0,//收发标志
                'cVouchType' => '11',//单据类型编码
                'cBusType' => $cBusType,
                'cSource' => $cSource,
                'cBusCode' => $data['source_code'],
                'cWhCode' => $data['wh_code'],
                'dDate'=>$data['order_date'],
                'cCode'=>$data['code'],//这里得用u8的单据号生成规则IC002201000175
                'cRdCode' => 22,
                'cDepCode' => $data['cDepCode'],//部门编码
                'cMemo' => isset($data['remark'])?$data['remark']:'',//表头备注
                'cMaker' =>session('admin')['u8_user_name'],//制单人 //u8用户名
                'VT_ID' => 35,
                'dnmaketime' => date('Y-m-d',time()), //制单日期
            ]);
        }else{
            $model->dnmodifytime = date('Y-m-d',time());
            $model->save();
        }
        return $model;
    }
}
